
package privatemessanger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This class will manage all of the register popup window, 2 sides- logo and user interface.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class RegPanel extends JPanel implements ActionListener
{
    private static String REGISTER= "Register";
    private static String CANCEL = "Cancel";
    private String _userName,_password;
    private LogoPanel _logoPanel;
    private UserPanel _userPanel;
    private JFrame controllingFrame; //needed for dialogs
    private MainFrame _mainFrame;

    /**
     * Setting up all the needed parameters.
     * @param f
     * @param RegFrame
     * @param mainFrame
     */
    public RegPanel( JFrame f,JFrame RegFrame, MainFrame mainFrame)
    {
         _mainFrame = mainFrame;
         controllingFrame = f;
         _logoPanel = new LogoPanel();
         _userPanel = new UserPanel(this,RegFrame, _mainFrame);

         f.getContentPane().add(_logoPanel, "West");
         f.getContentPane().add(_userPanel,"Center");

         _logoPanel.setBackground(Color.white);
         _userPanel.setBackground(Color.white);
    }

    /**
     * Get the username - string
     * @return UserName
     */
    public String getUserName(){ return _userName;}

    /**
     * Get the password- string
     * @return userPassword
     */
    public String getPassword(){ return _password;}

    /**
     * Setting up the userName
     * @param s
     */
    public void setUserName(String s){_userName= s;}

    /**
     * Setting up the user Password
     * @param s
     */
    public void setPassword(String s){_password= s;}

    public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); }

    /**
     * This class will manage the user interface side of the frame.
     */
    public class UserPanel extends JPanel implements ActionListener
    {
        private JTextField _userField;
        private JFrame _Regframe;
        private JPasswordField _passwordField,_confirmPass;
        private RegPanel _p;
        private MainFrame _mainFrame;

        /**
         * Setting up the wanted varaibles.
         * @param p
         * @param RegFrame
         * @param mainFrame
         */
        public UserPanel(RegPanel p,JFrame RegFrame, MainFrame mainFrame)
        {
            _mainFrame= mainFrame;
            _Regframe=RegFrame;
            _p = p;
            PaintComponent();
        }

        /**
         * Adding the components into the window in the correct order, so it will look AMAZING!!! yeahhh
         */
        private void PaintComponent()
        {
            JLabel _userName = new JLabel("Select a Username:  ");
            JLabel _linedown = new JLabel("this is a Line                                          Down:  ");
            _linedown.setForeground(Color.white);
            _p.add(_linedown);
            _linedown.setVisible(true);
            _p.add(_userName);
            _userName.setVisible(true);
            _p.setBackground(Color.white);
            _userField = new JTextField(12);
            _userField.addActionListener(this);
            _userField.requestFocusInWindow();
            _p.add(_userField);
            _userField.setVisible(true);

            _passwordField = new JPasswordField(12);
            _passwordField.setActionCommand(REGISTER);
            _passwordField.addActionListener(this);

            _confirmPass = new JPasswordField(12);
            _confirmPass.setActionCommand(REGISTER);
            _confirmPass.addActionListener(this);

            JLabel _pass = new JLabel("Choose a password: ");
            _pass.setBackground(Color.white);
            _pass.setLabelFor(_passwordField);

            JLabel _ConfirmPass = new JLabel("Confirm  password:   ");
            _ConfirmPass.setBackground(Color.white);
            _ConfirmPass.setLabelFor(_confirmPass);

            JComponent buttonPane = createButtonPanel();

             //Lay out everything.
            JPanel passField = new JPanel(new FlowLayout(FlowLayout.LEFT));
            passField.add(_pass);
            passField.add(_passwordField);

            _p.add(passField);

            JPanel ConfirmPassField = new JPanel(new FlowLayout(FlowLayout.LEFT));
            ConfirmPassField.add(_ConfirmPass);
            ConfirmPassField.add(_confirmPass);
            _p.add(ConfirmPassField);

            JLabel _linedown2 = new JLabel("this is a Line                                          Down:    ");
            _linedown2.setForeground(Color.white);
            _p.add(_linedown2);

            _p.add(buttonPane);
            _p.repaint();
        }

        /**
         * Create the button panel
         * @return the button panel
         */
        protected JComponent createButtonPanel()
        {
            JPanel p = new JPanel(new GridLayout(0,2));
            JButton regButton = new JButton("Register");
            JButton cancelButton = new JButton("Cancel");

            regButton.setActionCommand(REGISTER);
            cancelButton.setActionCommand(CANCEL);
            _Regframe.getRootPane().setDefaultButton(regButton);
            regButton.addActionListener(this);
            cancelButton.addActionListener(this);


            p.add(regButton);
            p.add(cancelButton);

            return p;
        }

        /**
         * If the user pressed the button, make the logic check on the input and make an action.
         * @param e
         */
         public void actionPerformed(ActionEvent e)
         {
            String cmd = e.getActionCommand();

            if (REGISTER.equals(cmd))
            {
                // if the user pressed on register
                int flag=0;
                //check username:
                if(_userField.getText().length()==0)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "Invalid Username. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }

                int isUsernameSpace=_userField.getText().indexOf(" ");
                if(isUsernameSpace != -1)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in username. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }

                //Process the password.
                char[] pass1 = _passwordField.getPassword();
                char[] pass2 = _confirmPass.getPassword();
                if(pass1.length != pass2.length || pass1.length==0 || pass2.length==0)
                {
                    JOptionPane.showMessageDialog(controllingFrame, "Invalid password. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                    return;
                }

                for(int j=0; j<pass1.length;j++)
                    if(pass1[j] == ' ')
                    {
                        JOptionPane.showMessageDialog(controllingFrame, "No Space Allowed in password. Try again.","Error Message",JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                for(int i=0; i<pass1.length;i++)
                    if (pass1[i] != pass2[i])
                    {
                        JOptionPane.showMessageDialog(controllingFrame,
                            "Invalid password. Try again.",
                            "Error Message",
                            JOptionPane.ERROR_MESSAGE);
                        flag = 1;
                        return;
                    }

                if(flag==0)
                {
                    char[] pass = _passwordField.getPassword();
                    String _s = "";
                    for (int i=0; i<pass.length;i++)
                        _s+=pass[i];

                    Client client = new Client(_userField.getText(), _s);
                    JOptionPane.showMessageDialog(controllingFrame, "Registration Successed !\n"+client.getUserName()+", your ID is: "+client.getID(),"Registration Successed",JOptionPane.INFORMATION_MESSAGE);
                    _mainFrame.regSucceed();
                    _mainFrame.setUser(client);
                    _mainFrame.setVisible(true);
                    _Regframe.dispose();
                }
            }
            else
            {
                //The user has pressed on Cancel.
                _Regframe.dispose();
                _mainFrame.setVisible(true);
            }
        }
    }

    /**
     * Setting up the logo side of the frame.
     */
    public class LogoPanel extends JPanel
    {
        private JLabel _logo;
        private JFrame _f;

        public LogoPanel()
        {
            setBackground(Color.white);
            ImageIcon _icon = createImageIcon("logo.jpg", "Register to Messenger now!");
            _logo = new JLabel(_icon);
            add(_logo);
            _logo.setVisible(true);
            repaint();
        }

        /**
         * Setting up the logo
         * @param path
         * @param description
         * @return
         */
        protected ImageIcon createImageIcon(String path, String description)
        {
            java.net.URL imgURL = LogoPanel.class.getResource(path);
            if (imgURL != null)
            {
                return new ImageIcon(imgURL, description);
            }
            else
            {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    }
}
